<!DOCTYPE html>
<html>
<head>
    <title>数据库管理系统</title>
    <link href="/Style/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/Style/css/bootstrap-icons.css">
    <style>
        body {
            padding-top: 2rem;
            padding-bottom: 2rem;
            background-color: #f8f9fa;
        }
        .table-card {
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .table-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .action-buttons {
            display: flex;
            gap: 0.5rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="mb-4 text-center">
            <h1 class="display-5"><i class="bi bi-database"></i> 数据库管理系统</h1>
            <p class="text-muted">管理数据库中的表</p>
        </div>

        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <div class="row row-cols-1 row-cols-md-3 g-4">
            {% for table in tables %}
            <div class="col">
                <div class="card table-card h-100">
                    <div class="card-body">
                        <h5 class="card-title d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-table me-2"></i>{{ table }}</span>
                            <span class="badge bg-primary">表</span>
                        </h5>
                        <p class="card-text">管理 {{ table }} 表中的数据记录</p>
                    </div>
                    <div class="card-footer bg-transparent">
                        <div class="action-buttons">
                            <a href="{{ url_for('sqlmanager_bp.table_view', table_name=table) }}"
                               class="btn btn-outline-primary flex-grow-1">
                                <i class="bi bi-eye me-1"></i> 查看表
                            </a>
                            <a href="{{ url_for('sqlmanager_bp.table_structure', table_name=table) }}"
                               class="btn btn-outline-info">
                                <i class="bi bi-gear me-1"></i> 结构
                            </a>
                            <button type="button"
                                    class="btn btn-outline-danger"
                                    data-bs-toggle="modal"
                                    data-bs-target="#dropTableModal"
                                    data-table-name="{{ table }}">
                                <i class="bi bi-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            {% else %}
            <div class="col-12">
                <div class="alert alert-info">
                    <i class="bi bi-info-circle me-2"></i>数据库中未发现任何表
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- 删除表确认模态框 -->
    <div class="modal fade" id="dropTableModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-exclamation-triangle text-warning me-2"></i>确认删除表</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>您确定要删除表 <strong id="modalTableName"></strong> 吗？此操作将删除表中所有数据，且无法撤销！</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <form id="dropTableForm" method="post">
                        <button type="submit" class="btn btn-danger">确认删除</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

     <script src="/Style/js/bootstrap.bundle.min.js"></script>
    <script>
        // 配置删除表模态框
        const dropTableModal = new bootstrap.Modal(document.getElementById('dropTableModal'));
        const dropTableForm = document.getElementById('dropTableForm');
        const modalTableName = document.getElementById('modalTableName');

        document.addEventListener('click', function(e) {
            if (e.target.closest('[data-bs-target="#dropTableModal"]')) {
                const btn = e.target.closest('[data-bs-target="#dropTableModal"]');
                const tableName = btn.getAttribute('data-table-name');

                // 修复URL拼接：使用正确的路由生成方式
                const baseUrl = "{{ url_for('sqlmanager_bp.drop_table_route', table_name='PLACEHOLDER') }}";
                // 将路由中的PLACEHOLDER替换为实际表名
                const actionUrl = baseUrl.replace('PLACEHOLDER', tableName);

                // 设置模态框内容和表单action
                modalTableName.textContent = tableName;
                dropTableForm.action = actionUrl;
            }
        });
    </script>
</body>
</html>